package org.nanotek.web.bean;

import java.util.Date;
import java.util.function.Function;
import java.util.function.Predicate;

import org.nanotek.Base;
import org.nanotek.base.person.Person;
import org.nanotek.base.person.name.NamePart;
import org.nanotek.base.person.name.NamePersonType;

@SuppressWarnings("serial")
public class PersonModel implements Base<Long> {

	protected Long id; 
	protected String firstName; 
	protected String lastName; 
	protected Date dateBirth;
	
	public PersonModel(){}
	
	public PersonModel(Person person)
	{ 
		postConstruct (person);
	}
	
	private void postConstruct(Person person) {
		this.id = person.getId();
		this.firstName = person.getNamePerson().getName().stream().filter(
				new NamePersonTypePredicate(NamePersonType.GivenName)).map(
						new  NamePartStringMapper()
						).findFirst().get();
		this.lastName = person.getNamePerson().getName().stream().filter(
				new NamePersonTypePredicate(NamePersonType.LastName)).map(
					new NamePartStringMapper() 
					).findFirst().get();
		this.dateBirth = person.getLifeCycle().getPersonDateBirth().getDate();
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public Date getDateBirth() {
		return dateBirth;
	}
	public void setDateBirth(Date dateBirth) {
		this.dateBirth = dateBirth;
	}
	
	class NamePersonTypePredicate implements  Predicate<NamePart>{
		
		private NamePersonType namePersonType;
		
		public NamePersonTypePredicate (NamePersonType namePersonType) 
		{ 
			this.namePersonType = namePersonType;
		}
		@Override
		public boolean test(NamePart t) {
			return t.getNamePersonType().equals(namePersonType);
		}
	}
	
	class NamePartStringMapper implements Function <NamePart,String>
	{

		@Override
		public String apply(NamePart t) {
			return t.getValue();
		}
		
		
	}
}
